package com.heima.wemedia.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.heima.apis.article.IArticleClient;

import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.StatisticsDto;
import com.heima.model.wemedia.dtos.WmStatisticDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.model.wemedia.vos.WmStatisticsVOs;
import com.heima.utils.common.DateUtils;
import com.heima.utils.thread.WmThreadLocalUtil;
import com.heima.wemedia.service.WmNewsService;
import com.heima.wemedia.service.WmStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@Slf4j
public class WmStatisticsServiceImpl implements WmStatisticsService {


    @Autowired
    private IArticleClient iArticleClient;

    @Autowired
    private WmNewsService wmNewsService;

    /**
     * 图文统计
     *
     * @param beginDate
     * @param endDate
     * @return
     */
    @Override
    public ResponseResult newsDimension(String beginDate, String endDate) {

        Map<String, Object> resultMap = new HashMap<>();

        //时间进行处理 (2023-07-25 2023-07-25)
        endDate=endDate.substring(0, endDate.lastIndexOf("-")+1)+(Integer.parseInt(endDate.substring(endDate.lastIndexOf("-")+1)) + 1);

        //类型转换  字符串转换为date类型
        Date beginDateTime = DateUtils.stringToDate(beginDate);
        Date endDateTime = DateUtils.stringToDate(endDate);



        WmUser user = WmThreadLocalUtil.getUser();

        //图文发布量
        int publishNum = wmNewsService.count(Wrappers.<WmNews>lambdaQuery()
                .eq(WmNews::getUserId, user.getId())
                .eq(WmNews::getStatus, WmNews.Status.PUBLISHED.getCode())
                .eq(WmNews::getEnable, 1)
                .between(WmNews::getPublishTime, beginDateTime, endDateTime));
        resultMap.put("publishNum", publishNum);

        //点赞数量&收藏数量
        WmStatisticDto wmStatisticDto = new WmStatisticDto(beginDateTime, endDateTime, user.getId());
        ResponseResult responseResult = iArticleClient.queryLikesAndCollections(wmStatisticDto);
        if (responseResult.getCode().equals(200) && responseResult.getData()!=null) {
            String string = JSON.toJSONString(responseResult.getData());
            WmStatisticsVOs counts = JSON.parseObject(string, WmStatisticsVOs.class);
            if (!Objects.isNull(counts)){
                resultMap.put("likesNum", counts.getLikes() == null ? 0 : counts.getLikes());
                resultMap.put("collectNum", counts.getCollections() == null ? 0 : counts.getCollections());
            }
        }
        return ResponseResult.okResult(resultMap);
    }

    /**
     * 分页查询图文统计
     *
     * @param dto
     * @return
     */
    @Override
    public PageResponseResult newsPage(StatisticsDto dto) {
        WmUser user = WmThreadLocalUtil.getUser();
        dto.setWmUserId(user.getId());
        PageResponseResult responseResult = iArticleClient.newPage(dto);
        return responseResult;
    }
}
